package shop.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import shop.model.User;
import shop.service.UserService;
import shop.vo.LogVo;
import shop.vo.ModVo;
import shop.vo.RegVo;

import javax.jws.soap.SOAPBinding;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String regUser(RegVo regVo){
        User user = new User(regVo.getUtele(),regVo.getUname(),regVo.getUpassword(),
                regVo.getUemail());
        if(userService.verUserpassword(regVo.getUpassword(),regVo.getUpassword2())){
            if(userService.regUser(user)){
                return "ok";
            }
        }
        return "err";
    }

    @PostMapping("/login")
    public String logUser(LogVo logVo) {

        User user = userService.logUser(
                logVo.getUtele(), logVo.getUpassword());
        if (user == null)
            return "err";
        else
            return "ok";
    }

    @PostMapping("/modify")
    public String modUser(ModVo modVo){
        if(userService.verUserpassword(
                modVo.getUpassword(),modVo.getUpassword2())){
            if(userService.modifyUserpassword(
                    modVo.getUtele(),modVo.getUpassword()))
                return "ok";
        }
        return "err";
    }


}
